<?php
namespace adminmatrix\admin\service;
use adminmatrix\admin\lists\BaseListsData;
use think\exception\HttpException;
use think\Response;
use think\Request;
use ReflectionFunction;
use ReflectionClass;
use think\response\Json;
use Closure;
use think\App;
use adminmatrix\admin\request\LoggableRequest;

class JsonService
{
    # 处理业务逻辑的回调函数
    private $handler;   

    protected Request $request;

    public function __construct(protected App $app)
    {
        $this->request = $app->request;
    }

    public function handle(callable $handler)
    {
        try {
            $this->handler = $handler;
            
            // 使用反射获取回调函数的参数信息
            $reflection = new ReflectionFunction($handler);
            $parameters = $reflection->getParameters();

            # 记录请求之前
            $prams = $this->request->param();

            $headers = $this->request->header();


            // 准备参数
            $args = [];
            foreach ($parameters as $parameter) {
                $paramType = $parameter->getType();
                if ($paramType === null) {
                    continue;
                }
                
                $typeName = $paramType->getName();
                // 从容器中获取对应类型的实例
                $args[] = app($typeName);
            }
            
            // 执行回调函数并传入解析的参数
            $result = call_user_func_array($handler, $args);
            
            # 存储过程
            /**
             * 存储
             * model 模型
             * 
             * 
             * 
             */
            $process = [];
            foreach($result as $key => $value){
                if($value instanceof \think\Model){
                    $process = $value;
                    break;
                }
            }
            

            // halt($prams , $headers , $process->getLastSql() , $result);    

           
          
            return self::success('success', $result);

            
        } catch(\think\exception\HttpException $e) {
            # Http异常
            return self::fail($e->getMessage());
        }
    }


    /**
     * 成功返回
     * @param string $message 消息
     * @param array|object $data 数据
     * @param int $code 状态码
     * @param int $show 是否显示
     * @param int $httpCode 返回的http状态码
     * @return Json
     */
    public static function success(string $message = "success", array|object $data = [], int $code = 1, int $show = 0 , int $httpCode = 200) : Json
    {

        return self::result($code , $message , $data , $show , $httpCode);
    }

    /**
     * 失败返回
     * @param string $message 消息
     * @param int $code 状态码
     * @param int $httpCode 返回的http状态码
     * @return Json
     */ 
    public static function fail(string $message = 'error' , int $code = 0 , int $httpCode = 200) : Json
    {
        return self::result($code , $message , [] ,0,$httpCode);
    }

    /**
     * 数据列表
     * @param BaseListsData $lists 数据列表
     * @return Json
     */
    public static function dataLists(BaseListsData $lists)
    {

        return self::result(1 , '', data:[
            'lists' =>  $lists->lists(),
            'total' =>  $lists->count(),
            'page'  =>  $lists->pageNo,
            'pageSize'  =>  $lists->pageSize
        ]);
    }

    /**
     * 返回结果
     * @param int $code 状态码
     * @param string $message 消息
     * @param array|object $data 数据
     * @param int $show 是否显示
     * @param int $httpCode 返回的http状态码
     * @return Json
     */
    private static function result(int $code, string $message, array|object $data = [], int $show = 0 , int $httpCode = 200) : Json
    {
        $result = compact("code" , 'message' , 'data' , 'show');
        return json($result , $httpCode);
    }
}


